//
//  NewTopicTableViewCell.m
//  miaopai
//
//  Created by duyifang on 15/11/13.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "NewTopicTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>

@interface NewTopicTableViewCell ()

@property(nonatomic, retain) UILabel *leftNumberLabel;

@property(nonatomic, retain) UIImageView *picImageView;

@property(nonatomic, retain) UILabel *topicTitleLabel;

@property(nonatomic, retain) UILabel *rightNumberLabel;

@property(nonatomic, retain) UIImageView *upOrDownImageView;

@end

@implementation NewTopicTableViewCell

- (void)dealloc
{
    [_leftNumberLabel release];
    [_picImageView release];
    [_topicTitleLabel release];
    [_rightNumberLabel release];
    [_upOrDownImageView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatSubViews];
    }
    return self;
}

- (void)creatSubViews
{
    self.leftNumberLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_leftNumberLabel];
    [_leftNumberLabel release];
    
    self.picImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_picImageView];
    [_picImageView release];
    
    self.topicTitleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_topicTitleLabel];
    [_topicTitleLabel release];
    
    self.rightNumberLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_rightNumberLabel];
    [_rightNumberLabel release];
    
    self.upOrDownImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_upOrDownImageView];
    [_upOrDownImageView release];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat numberLabelWidth = 23;
    _leftNumberLabel.frame = CGRectMake(10, (self.frame.size.height - numberLabelWidth) / 2, numberLabelWidth, numberLabelWidth);
    _leftNumberLabel.font = [UIFont fontWithName:@"Georgia" size:17];
    
    _picImageView.frame = CGRectMake(_leftNumberLabel.frame.size.width + _leftNumberLabel.frame.origin.x + 5, 5, self.frame.size.height - 10, self.frame.size.height - 10);
    _picImageView.backgroundColor = [UIColor yellowColor];
    
    _topicTitleLabel.frame = CGRectMake(_picImageView.frame.origin.x + _picImageView.frame.size.width + 10, (self.frame.size.height - 30) / 2, self.frame.size.width - (_picImageView.frame.origin.x + _leftNumberLabel.frame.size.width * 2 + 5) * 2 - 5, 30);
    
    _rightNumberLabel.frame = CGRectMake(_topicTitleLabel.frame.size.width + _topicTitleLabel.frame.origin.x + 5, _leftNumberLabel.frame.origin.y, _leftNumberLabel.frame.size.width, _leftNumberLabel.frame.size.height);
    _rightNumberLabel.textAlignment = NSTextAlignmentRight;
    
    _upOrDownImageView.frame = CGRectMake(_rightNumberLabel.frame.origin.x + _rightNumberLabel.frame.size.width + 5, _rightNumberLabel.frame.origin.y, _rightNumberLabel.frame.size.width, _rightNumberLabel.frame.size.height);
}

- (void)setNewsTopModel:(NewsTopicModel *)newsTopModel
{
    if (_newsTopModel != newsTopModel) {
        [_newsTopModel release];
        _newsTopModel = [newsTopModel retain];
    }
    
    NSInteger rank = [_newsTopModel.rank integerValue];
    if (rank < 4) {
        _leftNumberLabel.text = [NSString stringWithFormat:@"%.2ld", rank];
        _leftNumberLabel.textColor = [UIColor redColor];
    }else if (rank >= 4)
    {
        _leftNumberLabel.text = [NSString stringWithFormat:@"%.2ld", rank];
        _leftNumberLabel.textColor = [UIColor colorWithRed:0.5141 green:0.5141 blue:0.5141 alpha:1.0];
    }
    
    [_picImageView sd_setImageWithURL:[NSURL URLWithString:newsTopModel.topic.img] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    _topicTitleLabel.text = newsTopModel.topic.name;
    _topicTitleLabel.font = [UIFont systemFontOfSize:16];
    
    NSInteger riseNumber = [newsTopModel.rise integerValue];
    if (riseNumber < 0) {
        _rightNumberLabel.text = [NSString stringWithFormat:@"%ld", -riseNumber];
        [_upOrDownImageView setImage:[UIImage imageNamed:@"iconfont-xiajiantou"]];
          _rightNumberLabel.textColor = [UIColor colorWithRed:0.7286 green:0.7286 blue:0.7286 alpha:1.0];
    }else if (riseNumber == 0) {
        _rightNumberLabel.text = nil;
        [_upOrDownImageView setImage:NULL];
    }else
    {
        _rightNumberLabel.text = [NSString stringWithFormat:@"%ld", riseNumber];
        [_upOrDownImageView setImage:[UIImage imageNamed:@"iconfont-shangjiantou"]];
        _rightNumberLabel.font = [UIFont fontWithName:@"Georgia" size:17];
        _rightNumberLabel.textColor = [UIColor redColor];
    }

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
